﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using jSLCore.Client.Presentation.BaseControl;
using jSLCore.Client.Presentation.BaseControl.Selection;
using Telerik.Windows.Controls;

namespace jSLApp.Client.Wpf.Forms
{
    /// <summary>
    /// Interaction logic for TreeList.xaml
    /// </summary>
    public partial class TreeList : SelectControl
    {
        public TreeList()
        {
            InitializeComponent();

            var root = new node() { Name = "Root" };
            root.Children.Add(new node() { Name = "Child 1" });
            var child = new node() { Name = "Child 2" };
            child.Children.Add(new node() { Name = "SubChild1" });
            child.Children.Add(new node() { Name = "SubChild2" });
            child.Children.Add(new node() { Name = "SubChild3" });
            root.Children.Add(child);
            root.Children.Add(new node() { Name = "Child 3" });

            tree.ItemsSource = new[] { root };

            tree.SelectionMode = GridSelectionMode.SingleRow;
            
        }

        public override ISelectableGrid GetSelectableGrid()
        {
            return tree;
        }

        private void ended_grud(object sender, RadTreeViewDragEndedEventArgs e)
        {
            if(!e.IsCanceled)
                if (((node)e.TargetDropItem.DataContext).Name.Contains("Sub"))
                    {
                        e.Handled = true;
                    }
        }
    }


    public class node : INotifyPropertyChanged
    {
        private string _name;
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                InvokePropertyChanged("Name");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void InvokePropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }

        private ObservableCollection<node> _children = new ObservableCollection<node>();
        public ObservableCollection<node> Children
        {
            get { return _children; }
            set
            {
                _children = value;
                value.CollectionChanged += (sender, args) => InvokePropertyChanged("Children");
                InvokePropertyChanged("Children");
            }
        }
    }
}
